package com.scalar.forecast;

import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.Action;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;

import com.scalar.forecast.commons.MessageKeys;
import com.scalar.forecast.commons.SessionKeys;
import com.scalar.forecast.commons.Constants;
import com.scalar.forecast.forecastEntry.dao.ForecastEntryDAO;
import com.scalar.chiptrack.users.User;
import com.scalar.ScalarException;

/** This class extends the Action class in STRUTS Framework.
 *  All action classes in Forecast must extend this class.
 *
 * Created by Veeru. 
 * Date: Feb 8, 2005
 * Time: 11:47:50 AM 
 */


public abstract class ForecastExtendedAction extends Action implements MessageKeys, SessionKeys, Constants{


    protected HttpSession pSession = null;
    protected User pUser = null;

    /**
     * This method has to be implemented all action classes to provide business logic
     *
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public abstract ActionForward executeRequest( ActionMapping mapping,
                                                  ActionForm form,
                                                  HttpServletRequest request,
                                                  HttpServletResponse response) throws Exception;


    /**
     * This method is called by the controller servlet in order to transfer the control to the Action class
     * Its called for all requests sent to each and every action class
     *
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward execute( ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response )
        throws Exception
    {
        // Assign current session
        pSession = request.getSession();

        // Get current user
        pUser = ( User ) pSession.getAttribute( USER_KEY );

        // Redirect the user to login page, if he/she is not logged in
        if( pUser == null )
        {
            pSession.setAttribute( SOURCE_PAGE_KEY, request.getServletPath() + "?" + request.getQueryString() );
            return( mapping.findForward( ACTION_FORWARD_KEY_LOGIN ) );
        }

        // Create and add Messages HashMap to the request
        request.setAttribute( MESSAGES, new HashMap() );

        //get the salesPersonId of this user..
        String salesPersonId  = null;
        HashMap errors = new HashMap();
        try
        {
            salesPersonId = ForecastEntryDAO.getSalesPersonIdForUser ( null,pUser.getUserId() );
        }
        catch ( ScalarException e )
        {
            System.out.println ( "-----Exception while getting the salesPersonId-------" );
            e.printStackTrace();
            errors.put ( ERROR_INTERNAL_SERVER_ERROR, "Could Not load User SalesPersonId" );
            request.setAttribute ( SessionKeys.ERROR_MESSAGES, errors );
            return mapping.findForward ( "errorPage" );
        }
        pSession.setAttribute ( USER_SALESPERSON_ID, salesPersonId );

        // Send the request for actual processing
        return executeRequest( mapping, form, request, response );
    }
}
